package com.jingyuan.biz.server.testnetty;

import com.alibaba.fastjson.JSON;
import com.jingyuan.biz.server.EventRQ;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufUtil;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;
import io.netty.handler.codec.MessageToByteEncoder;
import io.netty.handler.codec.MessageToMessageEncoder;
import io.netty.util.CharsetUtil;
import org.springframework.boot.autoconfigure.jsonb.JsonbAutoConfiguration;

import java.nio.CharBuffer;
import java.nio.charset.Charset;
import java.util.List;

public class EventRQEncoder extends MessageToMessageEncoder<EventRQ> {

    @Override
    protected void encode(ChannelHandlerContext ctx, EventRQ eventRQ, List<Object> list) throws Exception {
        String str=JSON.toJSONString(eventRQ);
        System.out.println("发送的消息"+str);
        list.add(ByteBufUtil.encodeString(ctx.alloc(), CharBuffer.wrap(str), CharsetUtil.UTF_8));
    }
}
